﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_SummonSuccubus : TargetSpell
    {
        public Spell_SummonSuccubus() { }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (myCreatureBase is Character)
            {
                Character character = (Character)myCreatureBase;

                if (character.petManager.NumPets() > 3)
                {
                    Message msg = new Message();
                    msg.CreatureID = character.CreatureID;
                    msg.Type = "server";
                    msg.Text = "You have to many pets";

                    character.AddMessage(msg);

                    return;
                }

                List<Pet_Controller> pets = character.petManager.GetPets();
                foreach (Pet_Controller pet in pets)
                {
                    if (pet.MyCreature.ImageURL == "Succubus")
                    {
                        Message message = new Message();
                        message.Type = "server";
                        message.CreatureID = character.CreatureID;
                        message.Text = "You can not have more than 1 Succubus";

                        character.PlaySound("Ding", character.MapX, character.MapY);
                        character.AddMessage(message);

                        return;
                    }
                }
            }

            MapItem currentMapItem = null;
            foreach (MapItem mapItem in MapItemsWithin12)
            {
                if (mapItem.MapX == myCreatureBase.MapX && mapItem.MapY == myCreatureBase.MapY)
                {
                    currentMapItem = mapItem;
                    break;
                }
            }

            Creature succubus = new Creature(Managers.GameHandler);
            CreatureBase s = (CreatureBase)succubus;
            succubus.petController = new Pet_Controller(s, myCreatureBase, currentMapItem);
            succubus.petController.autoTauntAmount = (SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureInt) / 2) * -1;

            succubus.MaxHPBase = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureWis / 2) * 5;

            //succubus.MaxHPBase = System.Convert.ToInt32((myCreatureBase.MaxHP / 2)) + (myCreatureBase.WisdomTotal * 2);

            succubus.Agility = 2 * CastLevel;
            succubus.Armor = SkillManager.AdjustForCombat(CastLevel / 2, 0, CastingCreatureWis / 2);
            succubus.ResistUnholy = SkillManager.AdjustForCombat(CastLevel, 100, CastingCreatureWis);
            succubus.ResistFire = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);
            succubus.ResistIce = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);
            succubus.ResistMagic = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis);
            succubus.ResistPhysical = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);

            succubus.CorpseImageURL = "";
            succubus.CreatureID = Managers.GameHandler.ItemIDGen.GetCreatureUID();
            succubus.CreatureType = "Creature";

            succubus.Defense = CastingCreatureWis;
            succubus.DefenseSkill = CastLevel;

            succubus.HandBlockWeapon = "claw";
            succubus.Dexterity = myCreatureBase.IntelligenceTotal / 2;
            succubus.Faction = myCreatureBase.Faction;
            succubus.HP = succubus.MaxHPBase;
            succubus.ID = -1;
            succubus.Water = true;
            succubus.Air = false;
            succubus.Land = true;
            succubus.ImageURL = "Succubus";
            succubus.Instance = myCreatureBase.Instance;
            succubus.Intelligence = myCreatureBase.Intelligence / 2;

            succubus.MaxManaBase = myCreatureBase.MaxMana / 2;
            succubus.Mana = succubus.MaxManaBase;

            succubus.MapID = myCreatureBase.MapID;
            
            succubus.MaxMoves = 4;
            succubus.MovementSpeed = 3;

            succubus.MaxStaminaBase = myCreatureBase.MaxStamina / 3;
            succubus.Name = myCreatureBase.Name + "'s Succubus";
            succubus.SoundDeath = "SuccubusDeath";
            succubus.Stamina = succubus.MaxStaminaBase;
            succubus.Strength = myCreatureBase.IntelligenceTotal / 2;
            succubus.Wisdom = myCreatureBase.WisdomTotal / 2;

            succubus.Timer_Regen.Start();
            succubus.creatureAI.Timer_CreatureRound.Start();

            CreatureAttackWeapon succubusWeapon = new CreatureAttackWeapon();
            succubusWeapon.Accuracy = SkillManager.AdjustForCombat(CastLevel,0,CastingCreatureInt);
            succubusWeapon.Description = "claws {name}";
            succubusWeapon.DmgHigh = SkillManager.AdjustForCombat(CastLevel,0, System.Convert.ToInt32(CastingCreatureInt / 2)) + 15;
            succubusWeapon.DmgLow = SkillManager.AdjustForCombat(CastLevel,0, System.Convert.ToInt32(CastingCreatureInt / 2)) - 15;
            succubusWeapon.Frequency = 1;
            succubusWeapon.Sequence = 1;
            succubusWeapon.SkillLevel = CastLevel;
            succubusWeapon.Speed = 3.1M;

            succubus.attackSequenceManager.Attacks.Add(succubusWeapon);

            //Map CurrentMap = Managers.GameHandler.GetMap(myCreatureBase.MapID, myCreatureBase.Instance);

            MapSound soundToPlay = new MapSound();
            soundToPlay.Name = "SuccubusCast";
            soundToPlay.MapX = myCreatureBase.MapX;
            soundToPlay.MapY = myCreatureBase.MapY;

            MapItem mapitemToUpdate = myCreatureBase.currentMapItem;
            List<Character> CharsToUpdate = new List<Character>();
            foreach (MapItem mapitem in myCreatureBase.currentMapItem.mapItemsWithin8)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharsToUpdate.Add((Character)creaturebase);
                }
                if (mapitem.MapX == myCreatureBase.MapX && mapitem.MapY == myCreatureBase.MapY)
                {
                    succubus.SetPosition(mapitem);
                    mapitem.CreaturesBases.Add(succubus);
                    mapitemToUpdate = mapitem;
                }
            }
            foreach (Character character in CharsToUpdate)
            {
                character.PlaySound(soundToPlay.Name, soundToPlay.MapX, soundToPlay.MapY);
                character.AddMapItem(mapitemToUpdate);
                if (character == myCreatureBase)
                    character.petManager.AddPet(succubus.petController);
            }
        }
    }
}
